package com.jms.tomcat.registration;

import com.jms.tomcat.config.JmsFilterConfig;

import javax.servlet.*;
import java.util.*;

/**
 * Filter注册器
 */
public class JmsFilterRegistration implements FilterRegistration.Dynamic {

    /**
     * serlvet环境上下文
     */
    private final ServletContext servletContext;
    /**
     * Filter
     */
    private final Filter filter;
    /**
     * Filter配置信息
     */
    private final FilterConfig filterConfig;
    /**
     * Filter名称
     */
    private final String name;
    /**
     * Filter初始化参数
     */
    private Map<String, String> initParameters = new HashMap<>();
    /**
     * Filter映射
     */
    private final List<String> urlPatterens = new ArrayList<>();
    /**
     * Filter是否初始化完成
     */
    private boolean isInitialized = false;

    public JmsFilterRegistration(ServletContext servletContext, Filter filter, String name) {
        this.servletContext = servletContext;
        this.filter = filter;
        this.name = name;
        this.filterConfig = new JmsFilterConfig(this);
    }

    /**
     * 获取servlet环境上下文
     * @return ServletContext
     */
    public ServletContext getServletContext() {
        return this.servletContext;
    }

    /**
     * 获取Filter的config
     * @return ServletConfig
     */
    public FilterConfig getFilterConfig() {
        return filterConfig;
    }

    public boolean isInitialized() {
        return isInitialized;
    }

    public void setInitialized(boolean initialized) {
        isInitialized = initialized;
    }

    public Filter getFilter() {
        return filter;
    }

    @Override
    public Collection<String> getUrlPatternMappings() {
        return urlPatterens;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getClassName() {
        return this.getClass().getName();
    }

    @Override
    public boolean setInitParameter(String name, String value) {
        boolean result = initParameters.containsKey(name);
        initParameters.put(name, value);
        return result;
    }

    @Override
    public String getInitParameter(String name) {
        return initParameters.get(name);
    }

    @Override
    public Set<String> setInitParameters(Map<String, String> initParameters) {
        this.initParameters = initParameters;
        return initParameters.keySet();
    }

    @Override
    public Map<String, String> getInitParameters() {
        return initParameters;
    }

    @Override
    public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) {

    }

    @Override
    public Collection<String> getServletNameMappings() {
        return null;
    }

    @Override
    public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) {
        if (urlPatterns != null && urlPatterns.length > 0) {
            this.urlPatterens.addAll(Arrays.asList(urlPatterns));
        }
    }

    @Override
    public void setAsyncSupported(boolean isAsyncSupported) {

    }
}
